<?php

namespace app\admin\controller;

use app\common\controller\Agent;
use app\common\controller\Backend;
use app\common\model\Images;
use think\Db;
use think\Exception;

/**
 * 订单管理
 *
 * @icon fa fa-user
 */
class Order extends Backend
{

    protected $relationSearch = true;
    protected $multiFields = 'status';

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('Order');
    }

    /**
     * 查看
     */
    public function index()
    {
//        halt($this->agent_id);
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if ($this->request->isAjax()) {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('keyField')) {
                return $this->selectpage();
            }
            $filter = $this->request->get("filter", '');
            $filter = (array)json_decode($filter, true);
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $list = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->order($sort, $order)->paginate($limit);
            $count = [];
            if (empty($filter['order_status'])){ //全统计

                $quxiao = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->where('order_status',0)->order($sort, $order)->sum('pay_amount'); //已取消
                $xiaofei = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->where('order_status','in',[3,4])->order($sort, $order)->sum('pay_amount');//累计已核销（待评价、完成）
                $daihexiao = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->where('order_status','in',[2])->order($sort, $order)->sum('pay_amount');//累计待核销
                $tuikuan = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->where('order_status',5)->order($sort, $order)->sum('pay_amount');//退款
                $count = [
                    ['name'=>'累计已核销','value'=>$xiaofei],['name'=>'累计未核销','value'=>$daihexiao],['name'=>'累计取消','value'=>$quxiao],['name'=>'累计退款/售后','value'=>$tuikuan],

                ];
            }else{ //按类别统计
                if ($filter['order_status'] == 0){
                    $quxiao = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->where('order_status',0)->order($sort, $order)->sum('pay_amount'); //已取消
                    $count = [
                        ['name'=>'累计已核销','value'=>0],['name'=>'累计未核销','value'=>0],['name'=>'累计取消','value'=>$quxiao],['name'=>'累计退款/售后','value'=>0],
                    ];
                }elseif ($filter['order_status'] == 1){
                    $count = [
                        ['name'=>'累计已核销','value'=>0],['name'=>'累计未核销','value'=>0],['name'=>'累计取消','value'=>0],['name'=>'累计退款/售后','value'=>0],
                    ];
                }elseif ($filter['order_status'] == 5){
                    $tuikuan = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->where('order_status',5)->order($sort, $order)->sum('pay_amount');//退款
                    $count = [
                        ['name'=>'累计已核销','value'=>0],['name'=>'累计未核销','value'=>0],['name'=>'累计取消','value'=>0],['name'=>'累计退款/售后','value'=>$tuikuan],
                    ];
                }elseif ($filter['order_status'] == 2){
                    $xiaofei = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->where('order_status','in',[2])->order($sort, $order)->sum('pay_amount');//消费（待核销、待评价、完成）
                    $count = [
                        ['name'=>'累计已核销','value'=>0],['name'=>'累计未核销','value'=>$xiaofei],['name'=>'累计取消','value'=>0],['name'=>'累计退款/售后','value'=>0],
                    ];
                }elseif ($filter['order_status'] == 3){
                    $xiaofei = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->where('order_status','in',[3])->order($sort, $order)->sum('pay_amount');//消费（待核销、待评价、完成）
                    $count = [
                        ['name'=>'累计已核销','value'=>$xiaofei],['name'=>'累计未核销','value'=>0],['name'=>'累计取消','value'=>0],['name'=>'累计退款/售后','value'=>0],
                    ];
                }elseif ($filter['order_status'] == 4){
                    $xiaofei = $this->model->with(['member','shop','car','orderComment','agent'])->where($where)->where('order_status','in',[4])->order($sort, $order)->sum('pay_amount');//消费（待核销、待评价、完成）
                    $count = [
                        ['name'=>'累计已核销','value'=>$xiaofei],['name'=>'累计未核销','value'=>0],['name'=>'累计取消','value'=>0],['name'=>'累计退款/售后','value'=>0],
                    ];
                }
            }
            $result = array("total" => $list->total(), "rows" => $list->items(),'extend'=>['count'=>$count]);
            return json($result);
        }
        return $this->view->fetch();
    }


}
